package it.unipi.di.rasip.gui.chat;

import it.unipi.di.rasip.Rasip;
import it.unipi.di.rasip.utils.Color;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class ChatForm {
	TextField messageTf;
	ChatPanel panel;
	
	Vector messageList;
	
	public ChatForm() {
		messageList = new Vector();
		
		messageTf = new TextField(null, null, 200, TextField.ANY);
		
		Displayable d = Display.getDisplay(Rasip.getInstance()).getCurrent();
		int diff = 60;
		if (d instanceof Canvas) {
			diff += 45;
		}
		int w = d.getWidth();
		int h = d. getHeight() - diff;
		panel = new ChatPanel(w, h, messageList);
	}
	
	/**
	 * Appende al form padre gli elementi contenuti
	 * @param parentForm Form padre
	 */
	public void appendToForm(Form parentForm) {
		parentForm.append(panel);
		parentForm.append(messageTf);
	}

	public void addSelfMessage(String username, String stringMessage) {
		ChatMessage msg = new ChatMessage(username, stringMessage);
		msg.setUsernameColor(Color.RED);
		msg.setMessageColor(Color.BLACK);
		//messageList.addElement(msg);
		messageList.insertElementAt(msg, 0);
		panel.redraw();
	}

	public void addRemoteMessage(String username, String stringMessage) {
		ChatMessage msg = new ChatMessage(username, stringMessage);
		msg.setUsernameColor(Color.BLUE);
		msg.setMessageColor(Color.BLACK);
		//messageList.addElement(msg);
		messageList.insertElementAt(msg, 0);
		panel.redraw();
	}
}
